package com.justgame.project.generator.template.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.justgame.project.generator.container.EntityContainer;
import com.justgame.project.generator.enums.TemplateType;
import com.justgame.project.generator.properties.GlobalProperties;
import com.justgame.project.generator.properties.TemplateProperties;
import com.justgame.project.generator.schema.Entity;
import com.justgame.project.generator.schema.Enumerate;
import com.justgame.project.generator.schema.Field;
import com.justgame.project.generator.template.ITemplateResolve;
import com.justgame.project.generator.utils.NameUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

@Component
public class DomainEntityTemplateResolve implements ITemplateResolve {
    @Resource
    private TemplateProperties templateProperties;
    @Resource
    private GlobalProperties globalProperties;
    @Resource
    private EntityContainer entityContainer;

    @Override
    public TemplateType getType() {
        return TemplateType.ENTITY;
    }

    @Override
    public FileOutConfig buildConfig() {
        return new FileOutConfig(getTemplateFilePath()) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                tableInfoHandle(tableInfo);
                tableInfo.setConvert(false);
                return getOutputFilePath(tableInfo.getName());
            }
        };
    }

    @Override
    public String getTemplateFilePath() {
        Assert.hasLength(templateProperties.getEntityTemplatePath(),"请先配置 Entity 模板文件路径");
        return templateProperties.getEntityTemplatePath();
    }

    @Override
    public String getOutputFilePath(String tableName) {
        String moduleName = NameUtils.getModuleNameByTableName(tableName);
        String entityName = NameUtils.getEntityNameByTableName(tableName);
        return globalProperties.getProjectPath() + globalProperties.getAfterProjectDomainPath() +
                "/" + NameUtils.formatModuleServiceApiName(moduleName) + "/src/main/java"+ globalProperties.getDomainPackage() +"/"+moduleName+
                "/entity/" + entityName + StringPool.DOT_JAVA;
    }
}
